package com.maimang.platform.crm.service.impl;

import com.maimang.platform.crm.entity.ServiceExample;
import com.maimang.platform.crm.mapper.CustomerMapper;
import com.maimang.platform.crm.mapper.ServiceMapper;
import com.maimang.platform.crm.mapper.UserMapper;
import com.maimang.platform.crm.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Rocky
 * @date 2017/07/24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ServiceServiceImpl implements ServiceService {

	@Autowired
	private ServiceMapper serviceMapper;

	@Autowired
	private CustomerMapper customerMapper;

	@Autowired
	private UserMapper userMapper;

	@Override
	public long countByServiceExample(ServiceExample serviceExample) {
		return serviceMapper.countByExample(serviceExample);
	}

	@Override
	public boolean deleteByServiceExample(ServiceExample serviceExample) {
		return serviceMapper.deleteByExample(serviceExample) > 0;
	}

	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		return serviceMapper.deleteByPrimaryKey(id) > 0;
	}

	@Override
	public boolean insertService(com.maimang.platform.crm.entity.Service service) {
		service.setDeleteStatus(0);
		return serviceMapper.insert(service) > 0;
	}

	@Override
	public boolean insertSelective(com.maimang.platform.crm.entity.Service service) {
		return serviceMapper.insertSelective(service) > 0;
	}

	@Override
	public List<com.maimang.platform.crm.entity.Service> selectByServiceExample(ServiceExample serviceExample) {

		List<com.maimang.platform.crm.entity.Service> services = serviceMapper.selectByExample(serviceExample);
		//进行数据的二次封装
		for (com.maimang.platform.crm.entity.Service service : services) {
			if (service.getCreater() != null) {
				service.setCreaterObject(userMapper.selectByPrimaryKey(service.getCreater()));
			}
			if (service.getHandler() != null) {
				service.setHandlerObject(userMapper.selectByPrimaryKey(service.getHandler()));
			}
			if (service.getCustomerId() != null) {
				service.setCustomer(customerMapper.selectByPrimaryKey(service.getCustomerId()));
			}
		}
		return services;
	}

	@Override
	public com.maimang.platform.crm.entity.Service selectServiceByPrimaryKey(Integer id) {
		com.maimang.platform.crm.entity.Service service = serviceMapper.selectByPrimaryKey(id);
		if (service.getCreater() != null) {
			service.setCreaterObject(userMapper.selectByPrimaryKey(service.getCreater()));
		}
		if (service.getHandler() != null) {
			service.setHandlerObject(userMapper.selectByPrimaryKey(service.getHandler()));
		}
		if (service.getCustomerId() != null) {
			service.setCustomer(customerMapper.selectByPrimaryKey(service.getCustomerId()));
		}
		return service;
	}

	@Override
	public boolean updateByServiceExample(com.maimang.platform.crm.entity.Service service, ServiceExample serviceExample) {
		return serviceMapper.updateByExample(service, serviceExample) > 0;
	}

	@Override
	public boolean updateByServiceExampleSelective(com.maimang.platform.crm.entity.Service service, ServiceExample serviceExample) {
		return serviceMapper.updateByExampleSelective(service, serviceExample) > 0;
	}

	@Override
	public boolean updateServiceByPrimaryKey(com.maimang.platform.crm.entity.Service service) {
		return serviceMapper.updateByPrimaryKey(service) > 0;
	}

	@Override
	public boolean updateServiceByPrimaryKeySelective(com.maimang.platform.crm.entity.Service service) {
		return serviceMapper.updateByPrimaryKeySelective(service) > 0;
	}

	/**
	 * (non-Javadoc)
	 *
	 * @see ServiceService#deleteServicesByPrimaryKey(java.lang.String)
	 */
	@Override
	public boolean deleteServicesByPrimaryKey(String ids) {
		boolean success;
		try {
			//先把ids字符串根据规则分割开
			String[] idsStrings = ids.split("-");
			for (String idString : idsStrings) {
				Integer id = Integer.parseInt(idString);
				com.maimang.platform.crm.entity.Service service = new com.maimang.platform.crm.entity.Service();
				service.setId(id);
				service.setDeleteStatus(1);
				//如果删除失败，则直接抛出异常
				if (serviceMapper.updateByPrimaryKeySelective(service) < 1) {
					throw new RuntimeException();
				}
			}
			success = true;
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return success;
	}

}
